var AjaxFileUploadUtil = {
		checkExtension: function(source, url, reset){
			var value = source.value;
			if(value.length > 0){
				var parent = source.ancestors()[0];
				var formFile = $(source.form);
				formFile.disable();
				var extension = value.substring(value.lastIndexOf('.'),value.length);
				new Ajax.Request(url,{
					method:'get',
					parameters:{AjaxFileUploadExtensionParam:extension},
					onSuccess: function(resp){
						var respObj = resp.responseJSON;
						if(!respObj.success && reset){
							if (respObj.errorMessage) {
								value = '';
								alert(respObj.errorMessage);
							}
							parent.update(parent.innerHTML);
						}
						formFile.enable();
					},
					onFailure: function(resp){
						alert(resp.responseText);
						formFile.enable();
					}
				});
				
			}
		}
};
var AjaxFileUpload = Class.create();
AjaxFileUpload.prototype = {
		innerIframe: null,
		initialize: function(innerIframeId){
			var id = innerIframeId || false;
			if(id){
				this.innerIframe = $(id);
			}
		},
		
		frame : function(options, formId) {
			var n = 'f' + Math.floor(Math.random() * 99999);
			var d = new Element('DIV');
			d.update('<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="new AjaxFileUpload(\''+n+'\').loaded(\''+formId+'\')"></iframe>');
			document.body.appendChild(d);
	 
			this.innerIframe = $(n);
			if (options && typeof(options.onComplete) == 'function') {
				this.innerIframe.onComplete = options.onComplete;
			}
			return n;
		},
		
		form : function(f, name) {
			f.setAttribute('target', name);
		},
		
		submit : function(f, c) {
			this.form(f, this.frame(c, f.id));
			if (c && typeof(c.onStart) == 'function') {
				return c.onStart();
			} else {
				return true;
			}
		},
		
		loaded : function(formId) {
			var i = this.innerIframe;
			var d = null;
			if (i.contentDocument) {
				d = i.contentDocument;
			} else if (i.contentWindow) {
				d = i.contentWindow.document;
			} else {
				d = window.frames[id].document;
			}
			if (d.location.href == "about:blank") {
				return;
			}
			if (typeof(i.onComplete) == 'function') {
				i.onComplete(d, formId);
			}
		}

};
